using System;
using System.Collections.Generic;
using System.Linq;

using Csla;

using EntModApp.EntServices.Business;
using EntModApp.EntServices.IoC;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  [Serializable]
  public partial class CslaFavoriteEdit : 
    BusinessBase<CslaFavoriteEdit>,
    IFavorite
  {
    #region [ Business Properties ]

    public static PropertyInfo<double?> VotePropertyInfo = RegisterProperty(new PropertyInfo<double?>("Vote"));
    public double? Vote 
    { 
      get
      {
        return GetProperty(VotePropertyInfo);
      }
      set
      {
        LoadProperty(VotePropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaUserEdit> UserPropertyInfo = RegisterProperty(new PropertyInfo<CslaUserEdit>("User"));
    public CslaUserEdit User 
    { 
      get
      {
        return GetProperty(UserPropertyInfo);
      }
      private set
      {
        LoadProperty(UserPropertyInfo, value);
      }
    }

    public static PropertyInfo<Guid> UserIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("UserId"));
    public Guid UserId 
    { 
      get
      {
        return GetProperty(UserIdPropertyInfo);
      }
      private set
      {
        LoadProperty(UserIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSubjectEdit> SubjectPropertyInfo = RegisterProperty(new PropertyInfo<CslaSubjectEdit>("Subject"));
    public CslaSubjectEdit Subject 
    { 
      get
      {
        return GetProperty(SubjectPropertyInfo);
      }
      private set
      {
        LoadProperty(SubjectPropertyInfo, value);
      }
    }

    public static PropertyInfo<Guid> SubjectIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("SubjectId"));
    public Guid SubjectId 
    { 
      get
      {
        return GetProperty(SubjectIdPropertyInfo);
      }
      private set
      {
        LoadProperty(SubjectIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaFavoriteDerivedEditCollection> SuperFavoritesPropertyInfo = RegisterProperty(new PropertyInfo<CslaFavoriteDerivedEditCollection>("SuperFavorites"));
    public CslaFavoriteDerivedEditCollection SuperFavorites 
    { 
      get
      {
        return GetProperty(SuperFavoritesPropertyInfo);
      }
      private set
      {
        LoadProperty(SuperFavoritesPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Validation Rules ]
    #endregion [ Validation Rules ]

    #region [ Authorization Rules ]
    #endregion [ Authorization Rules ]
	
    #region [ Factory Methods ]

    #region [ Root Factory Methods ]
    public static CslaFavoriteEdit New()
    {
      return DataPortal.Create<CslaFavoriteEdit>();
    }

    public static void Delete(FavoriteKey key)
    {
      DataPortal.Delete<Favorite>(new SingleCriteria<Favorite, FavoriteKey>(key));
    }

    public static void Delete(Guid UserId, Guid SubjectId)
    {
      Delete(new FavoriteKey { UserId = UserId, SubjectId = SubjectId });
    }
    #endregion [ Root Factory Methods ]

    #region [ Child Factory Methods ]
    internal static CslaFavoriteEdit NewChild()
    {
      return DataPortal.CreateChild<CslaFavoriteEdit>();
    }

    internal static CslaFavoriteEdit GetFavoriteEdit(Favorite data, FavoriteLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaFavoriteEdit result;
      if (data is FavoriteDerived)
        result = CslaFavoriteDerivedEdit.GetFavoriteDerivedEdit((FavoriteDerived)data, FavoriteDerivedLoadSettings.FromFavoriteLoadSettings(loadSettings));
      else
      {
        result = new CslaFavoriteEdit();
        result.FetchFavorite(data, loadSettings);
      }
      result.MarkAsChild();
      return result;
    }
    #endregion [ Child Factory Methods ]

    private static int ForceInitField = 0;
    protected CslaFavoriteEdit() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]

    #region [ Root Data Access ]
    protected override void DataPortal_Create()
    {
      base.DataPortal_Create();
    }

    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Insert()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Update()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_DeleteSelf()
    {
      DataPortal_Delete(new SingleCriteria<Favorite, FavoriteKey>(new FavoriteKey { UserId = this.UserId, SubjectId = this.SubjectId }));
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    private void DataPortal_Delete(SingleCriteria<Favorite, FavoriteKey> criteria)
    {
    }
    #endregion [ Root Data Access ]

    #region [ Child Data Access ]
    protected override void Child_Create()
    {
      base.Child_Create();
    }
    
    private void Child_Fetch(object childData)
    {
    }
    
    private void Child_Insert(object parent)
    {
    }
    
    private void Child_Update(object parent)
    {
    }
    
    private void Child_DeleteSelf(object parent)
    {
    }
    #endregion [ Child Data Access ]

    protected void FetchFavorite(Favorite data, FavoriteLoadSettings loadSettings)
    {
      FetchFavoriteData(data, loadSettings);
      FetchFavoriteChildren(data, loadSettings);
    }

    protected void FetchFavoriteData(Favorite data, FavoriteLoadSettings loadSettings)
    {
      this.Vote = data.Vote;
      this.UserId = data.UserId;
      this.SubjectId = data.SubjectId;
    }

    protected void FetchFavoriteChildren(Favorite data, FavoriteLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeUser)
        this.User = CslaUserEdit.GetUserEdit(data.User, loadSettings.Inclusion.IncludedUserSettings);
      if (loadSettings.Inclusion.IncludeSubject)
        this.Subject = CslaSubjectEdit.GetSubjectEdit(data.Subject, loadSettings.Inclusion.IncludedSubjectSettings);
      if (loadSettings.Inclusion.IncludeSuperFavorites)
        this.SuperFavorites = CslaFavoriteDerivedEditCollection.GetFavoriteDerivedEditCollection(data.SuperFavorites, loadSettings.Inclusion.IncludedSuperFavoritesSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IFavorite implementation ]

    double? IFavorite.Vote
    {
      get { return this.Vote; }
      set
      {
        this.Vote = value;
      }
    }

    IUser IFavorite.User
    { 
      get
      {
        return this.User;
      }
      set
      {
        this.User = (CslaUserEdit)value;
      }
    }

    ISubject IFavorite.Subject
    { 
      get
      {
        return this.Subject;
      }
      set
      {
        this.Subject = (CslaSubjectEdit)value;
      }
    }

    IBusinessObjectCollection<IFavoriteDerived> IFavorite.SuperFavorites
    { 
      get
      {
        return this.SuperFavorites;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return false; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return this.IsDirty; }
    }

    #endregion [ Explicit IFavorite implementation ]
  }
}
